package array.leetcode.medium;

/**
 * @author bruin_du
 * @description 左右两边子数组的和相等
 * @date 2022/8/3 19:34
 **/
public class OfferII012_PivotIndex {
    //前缀和
    public int pivotIndex(int[] nums) {
        int len = nums.length, res = 0;
        int[] sums = new int[len + 2];
        for (int i = 1; i < sums.length - 1; i++) {
            sums[i] = sums[i - 1] + nums[i - 1];
        }
        for (int i = 0; i < len; i++) {
            if (sums[i] == sums[len] - sums[i + 1]) {
                return i;
            }
        }
        return -1;
    }
}
